//
//  SchoolNotificationController.m
//  NoNameAmazing
//
//  Created by 刘金壮 on 17/6/24.
//  Copyright © 2017年 北京执行力科技有限公司. All rights reserved.
//

#import "SchoolNotificationController.h"
#import "CommenWebViewController.h"
#import "NewsModel.h"
#import "NewsCell.h"

@interface SchoolNotificationController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic ,strong) NSMutableArray *dataSource;

@end

@implementation SchoolNotificationController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadNews];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"NewsCell" bundle:nil] forCellReuseIdentifier:@"NewsCell"];
}
- (void)loadNews{//获取新闻
    NSDictionary *params = @{@"name":@"news"};
    [NetWorkManager requestWithType:1 withUrlString:@"/plugin/getPluginByName.do" withParaments:params withSuccessBlock:^(id object) {
        if ([object[@"status"] integerValue] == 1) {
            NSString *url = object[@"pluginName"];
            NSString *urlStr = [NSString stringWithFormat:@"/%@/webService/InformationService/information/getAppInformation/%@",url,[us objectForKey:kRoleid]];
            [NetWorkManager requestWithType:0 withUrlString:urlStr withParaments:nil withSuccessBlock:^(id object) {
                if ([object[@"status"] integerValue] == 1) {
                    //                    http://s-learning.imwork.net:18080/新闻展示/webService/InformationService/information/getAllInformation/{lmh}/{roleid}/{page}
                    NSMutableArray *array = [NSMutableArray array];
                    for (NSDictionary *dict in object[@"columnList"]) {
                        [array  addObject:dict[@"lmh"]];
                    }
                    [NetWorkManager requestWithType:0 withUrlString:[NSString stringWithFormat:@"/%@/webService/InformationService/information/getAllInformation/%@/%@/0",url,array[2],[us objectForKey:kRoleid]] withParaments:nil withSuccessBlock:^(id object) {
                        if ([object[@"status"]integerValue] == 1) {
                            for (NSDictionary *dict in object[@"rows"]) {
                                NewsModel *model = [NewsModel modelWithDict:dict];
                                [self.dataSource addObject:model];
                            }
                            [self.tableView reloadData];
                        }
                    } withFailureBlock:^(NSError *error) {
                        
                    } progress:^(float progress) {
                        
                    }];
                }
            } withFailureBlock:^(NSError *error) {
                
            } progress:^(float progress) {
                
            }];
            
        }
    } withFailureBlock:^(NSError *error) {
        
    } progress:^(float progress) {
        
    }];
}
#pragma mark - tableViewDelegate -
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 91;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsCell"];
    cell.model = self.dataSource[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NewsModel *model = self.dataSource[indexPath.row];
    CommenWebViewController *web = [[CommenWebViewController alloc]init];
    web.urlStr = model.linkUrl;
    [self.navigationController pushViewController:web animated:YES];
}
#pragma mark - lazyInit -
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}



@end
